package admin

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"miaoyu/api/backend/utils"
	"miaoyu/internal/service"
	function "miaoyu/internal/utility"
)

var Common = new(commonCtl)

type commonCtl struct {
}

func (c *commonCtl) Captcha(ctx context.Context, req *utils.CaptchaDoReq) (res *utils.CaptchaDoRes, err error) {
	//parse request parameters
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))
	idKey, b64s, capErr := function.CaptchaGenerate()

	if capErr != nil {
		commonJson.SetError(capErr.Error())
	}

	// 返回结果数据
	res = &utils.CaptchaDoRes{
		B64s:  b64s,
		IdKey: idKey,
	}

	commonJson.SetSuccess("ok", res).WriteJsonExit()

	return
}

func (c *commonCtl) RefreshToken(ctx context.Context, req *utils.RefreshTokenDoReq) (res *utils.RefreshTokenDoRes, err error) {
	res = &utils.RefreshTokenDoRes{}
	res.Token, res.Expire = service.Auth().RefreshHandler(ctx)
	commonJson := service.JsonResp().CommonResp(g.RequestFromCtx(ctx))
	// 返回结果数据

	commonJson.SetSuccess("ok", res).WriteJsonExit()

	return
}
